import * as mine from "../../api/mine";
var appInst =  getApp();

Page({
    data: {
        list:[],
        qrcodeUrl:""
    },
    onLoad(options){

        mine.getQrcode({
            shopNo:options.shopNo
        })
        .then((res)=>{
            this.setData({
                qrcodeUrl:res
            })
        })
        // mine.getQrcodeList()
        // .then((res)=>{
            
        // })
    },
    // 保存图片
    saveImg: function (e) {

        let that = this;
        let imgSrc = e.currentTarget.dataset.item;

        wx.showLoading({
            title:"正在保存..."
        });
        //获取相册授权
        wx.getSetting({
            success(res) {
                if (!res.authSetting['scope.writePhotosAlbum']) {
                    wx.authorize({
                        scope: 'scope.writePhotosAlbum',
                        success() {
                            that.saveImgToLocal(imgSrc);
                        },
                        fail() {
                            wx.hideLoading();
                        }
                    })
                } else {//用户已经授权过了
                    that.saveImgToLocal(imgSrc);
                }
            }
        })

    },
    saveImgToLocal(imgSrc) {
        //var imgSrc = this.data.poster; //base64编码
        var save = wx.getFileSystemManager();
        var number = Math.random();

        wx.downloadFile({
            url: imgSrc,
            success: function (res) {

                //图片保存到本地
                wx.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                    success: function (data) {
                        wx.showToast({
                            title: '保存成功!',
                        })
                    },
                    fail: function (err) {
                        if (err.errMsg === "saveImageToPhotosAlbum:fail cancel") {
                            wx.showToast({
                                title: '保存失败!',
                                icon: 'none'
                            })
                        }
                    },
                })
            }
        })
    }
})
